package com.linkwedo.saas.directory;

import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;

import org.apache.commons.dbcp.BasicDataSource;

import com.linkwedo.saas.datasource.ConnectionInfo;
import com.linkwedo.saas.domain.member.User;
import com.linkwedo.saas.support.Security;

/**
 * LDAP 属性处理工厂
 * @author rabbit
 *
 */
public class AttributeFactory {
	private AttributeFactory(){
		throw new AssertionError();
	}
	
	public static Attributes createCompanyAttribute(String companyName, ConnectionInfo connectionInfo) {
		Attributes attrs = new BasicAttributes();
		Attribute objclass = new BasicAttribute("objectClass");
//		objclass.add("uidObject");
		objclass.add("organization");
		objclass.add("dbConnectionInfo");
		objclass.add("javaNamingReference");
		attrs.put(objclass);
		attrs.put("javaReferenceAddress", connectionInfo.getJndiURL());
		attrs.put("javaClassName", BasicDataSource.class.getName());
		attrs.put("o", companyName);
//		attrs.put("uid", connectionInfo.getUsername());
//		attrs.put("userPassword", connectionInfo.getPassword().getBytes());
		attrs.put("dbname", connectionInfo.getDatabaseName());
		attrs.put("dbpassword", connectionInfo.getPassword());
		attrs.put("dbusername", connectionInfo.getUsername());
		return attrs;
	}
	
	public static Attributes createUserAttributes(User user) {
		Attributes attrs = new BasicAttributes();
		Attribute objclass = new BasicAttribute("objectClass");
		objclass.add("inetOrgPerson");
		attrs.put(objclass);
		attrs.put("cn", user.getFirstName());
		attrs.put("sn", user.getLastName());
		attrs.put("uid", "" + user.getEmail());
		attrs.put("userPassword", Security.getApacheDSSHAEncoding(user.getPassword()));
		return attrs;
	}

}
